package net.huashitong.Indicators.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.huashitong.Indicators.entity.AreaCode;
import net.huashitong.until.Until;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GetDataProcessService {

    private static Logger log = LoggerFactory.getLogger(GetDataProcessService.class);

    @Autowired
    private AreaCodeService areaService;


    /**
     * 根据数据库获取时间列表
     * @param db
     * @return
     */
    public static List<String> getDataSj(String db) {
        List<String> sjList = new ArrayList<>();
        String dataSj = null;
        String url = "db=" + db;
        try {
            dataSj = Until.HttpGET(Until.GET_DATA_TIME + url, 5000, 5000);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("json数据解析失败");
        }
        JSONObject obj = JSONObject.parseObject(dataSj);
        if (200 == obj.getIntValue("returncode")) {
            JSONArray array = obj.getJSONArray("returndata");
            if (array.size() > 0) {
                for (int i = 0; i < array.size(); i++) {
                    String val = ((JSONObject) array.get(i)).getString("code");
                    sjList.add(val);
                }
            }else{
                System.out.println("没有时间");
            }
        }
        return sjList;
    }


    /**
     * 取得地区列表数据并保存
     * @param db
     * @return
     */
    public void saveDataReg(String db,String areaType){
        String areaP = null;
        String areaPP = null;
        JSONObject areaObject = null;
        List<AreaCode> arealist = new ArrayList<>();
        String url1 = "db=" + db;
        try {
            areaP = Until.HttpGET(Until.AREA_CODE_URL_DB_URL + url1, 5000, 5000);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("json数据解析失败");
        }
        JSONObject obj = JSONObject.parseObject(areaP);
        if (200 == obj.getIntValue("returncode")) {
            JSONArray array = obj.getJSONArray("returndata");
            String p = ((JSONObject) array.get(0)).getString("code");

            String url2 = "db=" + db + "&p=" + p;
            try {
                areaPP = Until.HttpGET(Until.AREA_CODE_URL_DB_URL + url2, 5000, 5000);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("json数据解析失败");
            }
            JSONObject obj2 = JSONObject.parseObject(areaPP);

            int returncode = obj2.getIntValue("returncode");
            if (200 == returncode) {
                array = obj2.getJSONArray("returndata");
                for (int i = 0; i < array.size(); i++) {
                    AreaCode areas = new AreaCode();
                    areaObject = (JSONObject) array.get(i);
                    areas.setParentCode(p);
                    areas.setAreaCode(areaObject.getString("code"));
                    areas.setAreaName(areaObject.getString("name"));
                    areas.setAreaType(areaType);
                    arealist.add(areas);
                }
            }
            areaService.batchSave(arealist);
            log.info(db+"地区数据，保存成功！");
        }
    }




}
